/*********************************************************************
显示时间、自定义内容
*********************************************************************/

#include "ssd1306_i2c.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#define MAXBUFSIZE 1024

static struct itimerval oldtv;

void set_timer()
{
    struct itimerval itv;
    itv.it_interval.tv_sec = 0;
    itv.it_interval.tv_usec = 500;
    itv.it_value.tv_sec = 0;
    itv.it_value.tv_usec = 500;
    setitimer(ITIMER_REAL, &itv, &oldtv);
}

time_t now;
struct tm *timenow;

void Display(int m)
{
    //清屏
    ssd1306_clearDisplay();

    //获取时间
    time(&now);
    timenow = localtime(&now);

    //显示时间
    ssd1306_drawString("    -  -      :  :");
    OLED_ShowNum(0, 0, timenow->tm_year + 1900, 4);
    OLED_ShowNumDetail(30, 0, timenow->tm_mon + 1, 2, '0');
    OLED_ShowNumDetail(48, 0, timenow->tm_mday, 2, '0');
    OLED_ShowNumDetail(62 + 12, 0, timenow->tm_hour, 2, '0');
    OLED_ShowNumDetail(78 + 12, 0, timenow->tm_min, 2, '0');
    OLED_ShowNumDetail(96 + 12, 0, timenow->tm_sec, 2, '0');
    ssd1306_drawString("\r\n\n");

    ssd1306_drawString("Hi, dudu \n\n");
    ssd1306_drawString("You are so beautiful!");

    ssd1306_display();
}

void main()
{
    // 屏幕初始化
    ssd1306_begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);

    // 绑定定时器执行函数
    signal(SIGALRM, Display);

    // 定时器启用中断  每0.5秒执行一次,就是2帧
    set_timer();

    while (1)
    {
    }
}
